<?php
require_once '../dao/MaskerDao.php';

$action = "";
if (isset($_GET['action'])) {
	$action = $_GET['action'];
}

switch ($action) {
	case 'checkMaskerAuth' :
		checkMaskerAuth();
		break;
	case 'anonymousMaskerAuth' :
		anonymousMaskerAuth();
		break;
	case 'checkMaskerExist':
		checkMaskerExist();
		break;
	case 'registerMasker':
		registerMasker();
		break;
	case 'infoMasker':
		infoMasker();
		break;
	default :
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no action found";
		print json_encode($jsonResult);
		break;
}

/*
 * get Masker info
 */
function infoMasker() {
	$maskerDao = new MaskerDao();
	$jsonResult = array();

	// check if name and password are isset
	if (!isset($_POST['name'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no name found";
	} else {
		$name = $_POST['name'];
		$record = $maskerDao -> getMaskerInfo($name);
		$jsonResult['result'] = $record;
	}

	print json_encode($jsonResult);
}

/*
 * Masker authentication check
 */
function checkMaskerAuth() {
	$maskerDao = new MaskerDao();
	$jsonResult = array();

	// check if name and password are isset
	if (!isset($_POST['name'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no name found";
	} else if (!isset($_POST['password'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no password found";
	} else {
		$name = $_POST['name'];
		$password = $_POST['password'];
		$record = $maskerDao -> checkMaskerAuth($name, $password);
		$jsonResult['result'] = $record != null ? "OK" : "KO";
		if ($record != null){
			session_start();
			$_SESSION["masker_id"] = $record["id"];
			$_SESSION["masker_name"] = $record["name"];
		}
	}

	print json_encode($jsonResult);
}

/*
 * Masker anonymous authentication
 */
function anonymousMaskerAuth() {
	$maskerDao = new MaskerDao();
	$jsonResult = array();

	$jsonResult['result'] = "OK";
	session_start();
	$_SESSION["masker_id"] = 0;
	$_SESSION["masker_name"] = "";

	print json_encode($jsonResult);
}

/*
 * Check if a masker name already exists
 */
function checkMaskerExist() {
	$maskerDao = new MaskerDao();
	$jsonResult = array();
	
	// check if name is isset
	if (!isset($_POST['name'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no name found";
	}  else {
		$name = $_POST['name'];
		$exist = $maskerDao -> existMasker($name);
		$jsonResult['result'] = ($exist);
	}

	print json_encode($jsonResult);
}	

/*
 * Check if a masker name already exists
 */
function registerMasker() {
	$maskerDao = new MaskerDao();
	$jsonResult = array();
	
	// check if name and password are isset
	if (!isset($_POST['name'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no name found";
	} else if (!isset($_POST['password'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no password found";
	} else if (!isset($_POST['mail'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no mail found";
	} else {
		$name = $_POST['name'];
		$password = $_POST['password'];
		$mail = $_POST['mail'];
		
		$result = $maskerDao -> insertMasker($name, $password, $mail);
		$jsonResult['result'] = $result;
	}
	print json_encode($jsonResult);
}
?>